from symbide import ModuleManager, LogManager
from keyword import kwlist    

def _Sympyplot(*args, **kwargs):
    if not ModuleManager.hasModule('sympy'):
        error = "SymPy is not available, therfore you can't use it's plotting module"
        LogManager.alert(error)
        def notavailable(*args):
            raise error
        return notavailable
    if not ModuleManager.hasModule('PIL'):
        error = "PIL is not available, therefore you can't use SymPy's plotting module"
        LogManager.alert(error)
        def notavailable(*args):
            raise error
        return notavailable
        
    from sympyplot import Sympyplot
    return Sympyplot(*args, **kwargs)
        
def _method_evalf(self, x):
    return x.evalf()

def _EasyFunction(*args):
    if ModuleManager.hasModule('sympy'):
        from easyFunction import EasyFunction
        return EasyFunction(*args)
    else:
        error = 'SymPy is not available, therfore you can\'t define SymPy functions'
        LogManager.alert(error)
        def notavailable(*args):
            raise error
        return notavailable
        
def _defineVariables(input, *args):
    '''
    Declare all not yet defined variables as sympy.Symbols
    '''
    if ModuleManager.hasModule('sympy'):
        from sympy import Symbol
        for i in args:
            if i in input.global_namespace.keys():
                continue
            if i in input.local_namespace.keys():
                continue
            if i in kwlist:
                continue
            if i in dir(__builtins__):
                continue
            input.local_namespace[i] = Symbol(str(i))